package navigation.wzg.com.navigation;

//注册页面

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Service;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Vibrator;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import navigation.wzg.com.navigation.Soaked.Soaked;
import navigation.wzg.com.navigation.Tools.Toolstotal;
import navigation.wzg.com.navigation.Write.Shareds;
import navigation.wzg.com.navigation.activities.Activity_menu_login;
import navigation.wzg.com.navigation.activities.Feedback;
import navigation.wzg.com.navigation.activities.Operation_diary;
import navigation.wzg.com.navigation.activities.Setup;
import navigation.wzg.com.navigation.activities.UpdateApp;
import navigation.wzg.com.navigation.activities.Update_diary;
import navigation.wzg.com.navigation.adapter.Badaper;
import navigation.wzg.com.navigation.db.Diary;
import navigation.wzg.com.navigation.db.greendao.DiaryDao;
import navigation.wzg.com.navigation.vo.AFaApplication;

public class MainActivity extends AppCompatActivity {
    int exit = 0;
    @BindView(R.id.ll)
    LinearLayout linear_bar;
    @BindView(R.id.list_item_main)
    ListView listitem;
    @BindView(R.id.write_diary)
    Button writeDiary;
    @BindView(R.id.exit_button)
    Button exitButton;
    @BindView(R.id.exitLayout)
    LinearLayout exitLayout;
    @BindView(R.id.activity_main)
    DrawerLayout activityMain;
    @BindView(R.id.main_toolbar)
    Toolbar main_Toolbar;
    @BindView(R.id.cela_out)
    ImageView celaOut;
    @BindView(R.id.welcome_text)
    TextView welcomeText;
    @BindView(R.id.username)
    TextView username;
    @BindView(R.id.lookuoEdittext)
    EditText lookuoEdittext;
    @BindView(R.id.lookuoLayout)
    LinearLayout lookuoLayout;

    private DiaryDao dao;
    private List<Diary> listsUser;
    private Badaper adapter;
    private LinearLayout button_src;
    private LinearLayout button_feed;
    private LinearLayout button_shehzi;
    private Button button_exit;
    DrawerLayout d;
    public static Activity activityas;
    boolean islookup = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

        //跑马灯效果
        welcomeText.setSelected(true);

        String[] a = Shareds.Reads_username(this);
        username.setText(a[0]);

        activityas = this;
        sousuo();
        celadianji();
        setSupportActionBar(main_Toolbar);
        ActionBar actionBar = getSupportActionBar();
        actionBar.setTitle("");

        //沉浸状态栏
        Soaked.initState(this, linear_bar);
        d = (DrawerLayout) findViewById(R.id.activity_main);
        dao = AFaApplication.getApplication().getDaoSession().getDiaryDao();
        //
        listitem.setOnItemClickListener(new MyOnItemClickListener());
        //listview 长按事件
        listitem.setOnItemLongClickListener(new MyOnItemLongClickListener());

        listsUser = dao.loadAll();
        adapter = new Badaper(this, listsUser);
        listitem.setAdapter(adapter);


        /**
         * toolbar中menu菜单的监听和匹配点击事件
         * TODO toolbar中menu菜单的监听和匹配点击事件
         */

        main_Toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                if (item.getItemId() == R.id.lookup) {
                    if (islookup == false){
                        lookuoLayout.setVisibility(View.VISIBLE);
                        islookup = true;
                    }else {
                        lookuoLayout.setVisibility(View.GONE);
                        islookup = false;
                    }

                } else if (item.getItemId() == R.id.add) {
                    startActivity(new Intent(MainActivity.this, Activity_menu_login.class));
                    overridePendingTransition(R.anim.out, R.anim.up);
                } else if (item.getItemId() == R.id.addgoto) {
                    finish();
                    startActivity(new Intent(MainActivity.this, Welcome.class));
                    Shareds.First("item", true, MainActivity.this);
                } else if (item.getItemId() == R.id.haoju) {
                    Toolstotal.share(MainActivity.this);
                    Toast.makeText(MainActivity.this, "分享", Toast.LENGTH_SHORT).show();
                } else if (item.getItemId() == R.id.about) {
                    new AlertDialog.Builder(MainActivity.this)
                            .setTitle("关于:")
                            .setMessage("\n我是一个日记本\n\n")
                            .setNegativeButton("检查更新", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                    startActivity(new Intent(MainActivity.this, UpdateApp.class));
                                    overridePendingTransition(R.anim.out, R.anim.up);
                                }
                            })
                            .setPositiveButton("同意", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {

                                }
                            })
                            .show();
                    overridePendingTransition(R.anim.out, R.anim.up);
                }
                return true;
            }
        });
    }

    /**
     * menu菜单的绑定代码，与上面的setSupportActionBar(main_Toolbar)需要同时存在
     *
     * @param menu
     * @return
     */
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main_menu, menu);//加载menu文件到布局
        return true;
    }

    //TODO 菜单显示，隐藏；
    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        if (Shareds.Firsttrue("item", MainActivity.this)) {
            menu.findItem(R.id.addgoto).setVisible(false);
            menu.findItem(R.id.add).setVisible(true);
        } else {
            menu.findItem(R.id.addgoto).setVisible(true);
            menu.findItem(R.id.add).setVisible(false);
        }
        return true;
    }


    /**
     * 监听返回键，显示退出弹窗
     */
    public void onBackPressed() {
        if (d.isDrawerOpen(Gravity.LEFT)) {
            d.closeDrawer(Gravity.LEFT);
        } else {
            if (exit == 0) {
                exitLayout.setVisibility(View.VISIBLE);
                exit++;
            } else {
                exitLayout.setVisibility(View.GONE);
                exit--;
            }
        }
    }

    //TODO 事件监听
    //    监听
    @OnClick({R.id.write_diary, R.id.exit_button, R.id.cela_out})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.write_diary:
                startActivity(new Intent(MainActivity.this, Operation_diary.class));
                finish();
                overridePendingTransition(R.anim.out, R.anim.up);
                break;
            case R.id.exit_button:
                finish();
                overridePendingTransition(R.anim.out, R.anim.up);
                break;
            case R.id.cela_out:
                d.openDrawer(Gravity.LEFT);
                break;
        }
    }

    //TODO Listview短按
    //短按
    class MyOnItemClickListener implements AdapterView.OnItemClickListener {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            zd();

//带值跳转
            Intent intent = new Intent();
            intent.setClass(MainActivity.this, Update_diary.class);
            Diary listid = listsUser.get(i);
            Bundle bundle = new Bundle();
            bundle.putLong("id", listid.getId());
            bundle.putString("matter", listid.getMatter());
            bundle.putString("time", listid.getTime());
            intent.putExtras(bundle);
            startActivity(intent);
            finish();
            overridePendingTransition(R.anim.out, R.anim.up);
        }
    }

    //TODO Listview长按
    //长按
    class MyOnItemLongClickListener implements AdapterView.OnItemLongClickListener {

        @Override
        public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
            zd();
            final Diary listbox = listsUser.get(i);
            new AlertDialog.Builder(MainActivity.this)
                    .setTitle("通知")
                    .setMessage("删除？")
                    .setNegativeButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            //删除操作
                            dao.delete(listbox);
                            //刷新list
                            adapter.setData(dao.loadAll());
                        }
                    }).setPositiveButton("取消", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {

                }
            }).show();
            return true;
        }
    }

    /**
     * 侧拉框中的点击事件
     * 单独写出来
     * TODO 恻拉框点击事件
     */
    public void celadianji() {
        button_src = (LinearLayout) findViewById(R.id.button_src);
        button_src.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                d.closeDrawer(Gravity.LEFT);
                Toast.makeText(MainActivity.this, "主页", Toast.LENGTH_SHORT).show();
            }
        });
        button_feed = (LinearLayout) findViewById(R.id.button_feed);
        button_feed.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(MainActivity.this, Feedback.class));
                overridePendingTransition(R.anim.out, R.anim.up);
                d.closeDrawer(Gravity.LEFT);
                Toast.makeText(MainActivity.this, "反馈", Toast.LENGTH_SHORT).show();
            }
        });
        button_shehzi = (LinearLayout) findViewById(R.id.button_shehzi);
        button_shehzi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(MainActivity.this, Setup.class));
                d.closeDrawer(Gravity.LEFT);
                overridePendingTransition(R.anim.out, R.anim.up);
            }
        });
        button_exit = (Button) findViewById(R.id.button_exit);
        button_exit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
                overridePendingTransition(R.anim.out, R.anim.up);
            }
        });
    }


    //震动
    public void zd() {
        Vibrator vibrator = (Vibrator) getSystemService(Service.VIBRATOR_SERVICE);
        vibrator.vibrate(new long[]{0, 100}, -1);
    }

    public void sousuo(){
        lookuoEdittext.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                Toast.makeText(MainActivity.this, lookuoEdittext.getText().toString(), Toast.LENGTH_SHORT).show();
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
    }
}
